package com.eco.fanliapp.job.sytimeperiodgoodsjob;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eco.fanliapp.entity.SyTimePeriodGoods;
import com.eco.fanliapp.service.IAppGoodsService;
import com.eco.fanliapp.service.ISyTimePeriodGoodsService;
import com.eco.fanliapp.utils.DateFormatUtils;
import org.apache.commons.collections.CollectionUtils;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 限时秒杀商品归位
 * 1.查询昨天带秒杀标签的商品
 * 2.判断是否在今天秒杀商品中
 * 3.剩下的商品将标签状态更改成删除
 *
 */
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class Task2 implements Job, Serializable {


    private static final Logger log = LoggerFactory.getLogger(Task2.class);

    @Autowired
    private ISyTimePeriodGoodsService syTimePeriodGoodsService;
    @Autowired
    private IAppGoodsService appGoodsService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        //取得job详情
        JobDetail jobDetail = context.getJobDetail();

        JobDataMap dataMap = jobDetail.getJobDataMap();

        int executeCount = 0;
        if (dataMap.containsKey("runCount")) {
            executeCount = dataMap.getInt("runCount");
        }

        // 增量计数并将其存储回 JobDataMap，这样可以适当保持工作状态
        executeCount++;
        dataMap.put("runCount", executeCount);

        long startTime1 = System.currentTimeMillis();
        log.info("***************************************开始执行限时秒杀商品归位任务-Task2：executeCount=" + executeCount + "--startTime=" + startTime1);

        //获取当前日期前一天
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        String endTime = DateFormatUtils.getNowDateDay(cal.getTime());
        log.info("获取当前日期前一天{}",endTime);
        //获取昨天秒杀商品
        QueryWrapper<SyTimePeriodGoods> query = new QueryWrapper<>();
        query.between("period_goods_time",endTime + " 00:00:00",endTime + " 11:59:59");
        query.eq("enable",1);
        query.eq("deleted",0);
        query.select("item_id");
        List<SyTimePeriodGoods> list = syTimePeriodGoodsService.list(query);

        List<Long> lt = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(list)){
            for(SyTimePeriodGoods goods:list){
                lt.add(goods.getItemId());
            }
        }

        //获取当前日期秒杀商品
        Calendar cal1= Calendar.getInstance();
        cal1.add(Calendar.DATE,0);
        String endTime1 = DateFormatUtils.getNowDateDay(cal1.getTime());
        log.info("获取当前日期秒杀商品{}",endTime1);
        //获取昨天秒杀商品
        QueryWrapper<SyTimePeriodGoods> query1 = new QueryWrapper<>();
        query1.between("period_goods_time",endTime1 + " 00:00:00",endTime1 + " 11:59:59");
        query1.eq("enable",1);
        query1.eq("deleted",0);
        query1.select("table_id,item_id");
        List<SyTimePeriodGoods> list1 = syTimePeriodGoodsService.list(query1);

        List<Long> lt1 = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(list1)){
            for(SyTimePeriodGoods goods:list1){
                lt1.add(goods.getItemId());
            }
        }

        //移除昨天秒杀商品中，今天还秒杀的数据
        lt.removeAll(lt1);
        //删除秒杀标签中的商品
        if(CollectionUtils.isNotEmpty(lt)){
            log.info("执行批量更新操作");
            syTimePeriodGoodsService.updateBatch(lt);
        }

        long endTime2 = System.currentTimeMillis();
        log.info("***************************************结束执行限时秒杀商品归位任务-Task2：executeCount=" + executeCount + "--endTime=" + endTime + "--耗时=" + (endTime2 - startTime1));

    }


    public static void main(String[] args) {
//        //获取当前日期前一天
//        Calendar cal=Calendar.getInstance();
//        cal.add(Calendar.DATE,0);
//
//        String endTime = DateFormatUtils.getNowDateDay(cal.getTime());
//        log.info("执行归位日期为{}",endTime);

        List<String> list1  = new ArrayList<>();
//        list1.add("a");
//        list1.add("b");
        list1.add("c");
        List<String> list2  = new ArrayList<>();
        list2.add("b");
        list2.add("c");
        list2.add("f");
        list2.add("e");


        list1.removeAll(list2);

        for(String s:list1){
        }
    }
}
